package com.pine.app.module.security.core.authorize.intercept;

import com.pine.app.module.security.core.authorize.annotation.ResourceAuthorize;
import com.pine.app.module.security.core.authorize.annotation.RoleAuthorize;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.InitBinder;

import java.lang.reflect.Method;

/**
 * @author xiaoyuan
 * @create 2019/9/2 13:44
 **/


@Slf4j
public class MethodAuthorizeAspect extends StaticMethodMatcherPointcutAdvisor implements InitializingBean {



    private MethodAuthorizeInterceptor sysLogInterceptor;


//    public MethodAuthorizeAspect(MethodAuthorizeInterceptor sysLogInterceptor){
//        this.sysLogInterceptor = sysLogInterceptor;
//    }


    @Autowired
    public void setSysLogInterceptor(MethodAuthorizeInterceptor sysLogInterceptor) {
        this.sysLogInterceptor = sysLogInterceptor;
    }

    @Override
    public boolean matches(Method method, Class<?> aClass) {
        if((aClass.getAnnotation(ResourceAuthorize.class)!=null||method.getAnnotation(ResourceAuthorize.class)!=null)&&
        AnnotationUtils.findAnnotation(method, InitBinder.class)==null){
            return true;
        }
        if((method.getAnnotation(RoleAuthorize.class)!=null||aClass.getAnnotation(RoleAuthorize.class)!=null)&&
                AnnotationUtils.findAnnotation(method, InitBinder.class)==null){
            return true;
        }
        return false;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        this.setAdvice(sysLogInterceptor);
    }
}
